package com.macro.mall.portal.controller.ums;

import com.macro.mall.commission.entity.UserAccountEntity;
import com.macro.mall.commission.service.OrderPredicateService;
import com.macro.mall.commission.service.UserAccountService;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.util.BigDecimalUtils;
import com.macro.mall.common.api.util.LocalDateTimeUtils;
import com.macro.mall.model.UmsMember;
import com.macro.mall.portal.controller.BaseController;
import com.macro.mall.portal.vo.ums.MyVO;
import com.macro.mall.ums.domain.Member;
import com.macro.mall.ums.entity.MemberEntity;
import com.macro.mall.ums.service.account.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * class
 *
 * @author xuyanjun
 * @date 2019-12-11
 */
@Api(tags = "MemberController", description = "会员信息管理")
@Slf4j(topic = "MemberController")
@RestController
public class MemberController extends BaseController {

    @Autowired
    private MemberService memberService;

    @Autowired
    private OrderPredicateService orderPredicateService;

    @Autowired
    private UserAccountService userAccountService;

    /**
     * 获取当前会员信息
     */
    @ApiOperation("获取当前会员信息")
    @PreAuthorize("isAuthenticated()")
    @GetMapping(value = "/member/info")
    public CommonResult<Member> info() {
        UmsMember currentUser = getCurrentUser();

        MemberEntity memberEntity = memberService.getById(currentUser.getId());
        Member member = memberService.entityToDomain(memberEntity);

        return CommonResult.success(member);
    }

    /**
     * 获取当前会员统计信息
     */
    @ApiOperation("获取当前会员信息")
    @PreAuthorize("isAuthenticated()")
    @GetMapping(value = "/member/my")
    public CommonResult<MyVO> my() {
        UmsMember currentUser = getCurrentUser();

        MyVO myVO = new MyVO();
        setPredictCommission(currentUser.getId(), myVO);

        long countOfFans = memberService.countOfFans(currentUser.getId());
        myVO.setFansCount(countOfFans);

        return CommonResult.success(myVO);
    }


    /**
     * 设置预估佣金
     *
     * @param myVo myVo
     */
    private void setPredictCommission(long userId, MyVO myVo) {
        UserAccountEntity userAccountEntity = userAccountService.getByUserId(userId);

        // 今日预估
        LocalDateTime minTimeOfToday = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime maxTimeOfToday = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);

        long predictOfToday = orderPredicateService.sumAmountByCreateTimeOfUser(userId, minTimeOfToday, maxTimeOfToday);
        myVo.setPredictOfToday(BigDecimalUtils.fromFengToYuan((int) predictOfToday, 2).toString());

        // 昨日预估
        LocalDateTime minTimeOfYesterday = minTimeOfToday.plusDays(-1);
        LocalDateTime maxTimeOfYesterday = maxTimeOfToday.plusDays(-1);

        long predictOfYesterday = orderPredicateService.sumAmountByCreateTimeOfUser(userId, minTimeOfYesterday, maxTimeOfYesterday);
        myVo.setPredictOfYesterday(BigDecimalUtils.fromFengToYuan((int) predictOfYesterday, 2).toString());

        // 本月预估
        LocalDateTime minTimeOfThisMonth = LocalDateTimeUtils.getFirstDateOfMonth(LocalDateTime.now());
        LocalDateTime maxTimeOfThisMonth = LocalDateTimeUtils.getLastDateOfMonth(LocalDateTime.now());

        long predictOfThisMonth = orderPredicateService.sumAmountByCreateTimeOfUser(userId, minTimeOfThisMonth, maxTimeOfThisMonth);
        myVo.setPredictOfThisMonth(BigDecimalUtils.fromFengToYuan((int) predictOfThisMonth, 2).toString());

        // 总预估
        long predictOfTotal = userAccountEntity == null ? 0 : userAccountEntity.getTotal();
        myVo.setPredictOfTotal(BigDecimalUtils.fromFengToYuan((int) predictOfTotal, 2).toString());

        // 账户余额
        int balance=userAccountEntity==null?0:userAccountEntity.getBalance();
        myVo.setBalance(BigDecimalUtils.fromFengToYuan(balance, 2).toString());
    }
}
